package com.csiec.clock.view.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.util.Log;

import androidx.lifecycle.ViewModel;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.csiec.clock.adpter.MyJoinClockAdapter;
import com.csiec.clock.bean.myJoinClock.JoinClock;
import com.csiec.clock.bean.myJoinClock.ResultCode;
import com.csiec.clock.callback.MyJoinClockCallBack;
import com.csiec.clock.databinding.ActivityJoinClockBinding;
import com.csiec.clock.model.JoinModel;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.List;

public class JoinClockViewModel extends ViewModel {
    @SuppressLint("StaticFieldLeak")
    private Activity activity;
    private ActivityJoinClockBinding binding;

    private JoinModel model;
    private final Handler handler = new Handler();
    private List<JoinClock> clockList;

    public void init(Activity activity, ActivityJoinClockBinding binding) {
        model = new JoinModel();
        this.activity = activity;
        this.binding = binding;
        getClock();

    }

    //获取用户参与打卡列表
    public void getClock() {
        model.getPunchUrl(new MyJoinClockCallBack() {

            @Override
            public void onSuccess(ResultCode backData) {
                if (backData.getStatus() == 200) {
                    clockList = backData.getData().getData();
                    for (JoinClock datum : backData.getData().getData()) {
                        Log.d("JoinClockViewModel", "onSuccess: " +datum.toString());
                    }

                    handler.post(() -> initView());
                }
            }

            @Override
            public void onFailed() {
                handler.post(() -> {
                    showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络异常");
                });
            }
        });

    }

    private void initView() {
        binding.recyclerView3.setLayoutManager(new LinearLayoutManager(activity.getApplication()));
        if (clockList != null) {
            MyJoinClockAdapter adapter = new MyJoinClockAdapter(clockList, activity);
            binding.recyclerView3.setAdapter(adapter);
        }
    }

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.recyclerView3.postDelayed(tipDialog::dismiss, 2000);
    }
}
